from PIL import Image
import numpy as np
import cv2


path = r'CorrectFiles/right3_new2.jpg'
img = Image.open(path)
img = np.array(img)


def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        xy = "%d,%d" % (x, y)
        # points.append((x,y))
        # if len(points) == 2:
        #     min_row = min(points[0][1], points[1][1])
        #     max_row = max(points[0][1], points[1][1])
        #     min_cul = min(points[0][0], points[1][0])
        #     max_cul = max(points[0][0], points[1][0])
        #     cv2.imwrite(r'G:\nishiliu\cut.jpg', img[min_row:max_row, min_cul:max_cul,:])

        print(xy)
        txt = xy
        cv2.circle(img, (x, y), 1, (255, 0, 0), thickness=-1)
        cv2.putText(img,txt , (x, y), cv2.FONT_HERSHEY_PLAIN,
                    1.0, (0, 0, 0), thickness=1)
        cv2.imshow("image", img)

# img1 = img[:img.shape[0]//2,:img.shape[1]//2]
cv2.namedWindow("image", 0)
cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
cv2.imshow("image", img)

while (True):
    try:
        cv2.waitKey(100)
    except Exception:
        cv2.destroyWindow("image")
        break

cv2.waitKey(0)
cv2.destroyAllWindow()